package com.tuanzhang.mall.seller.application.impl;

import com.tuanzhang.mall.seller.application.SellerApplicationService;
import com.tuanzhang.mall.seller.domain.aggregate.Seller;
import com.tuanzhang.mall.seller.domain.service.SellerService;
import com.tuanzhang.mall.seller.domain.valueobj.CompanyInfo;
import com.tuanzhang.mall.seller.domain.valueobj.Email;
import com.tuanzhang.mall.seller.domain.valueobj.PhoneNumber;
import com.tuanzhang.mall.seller.infrastructure.repository.SellerRepository;
import com.tuanzhang.mall.seller.types.req.UserInfoParam;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

/**
 * 买家应用类
 *
 * @author wangyl
 * @date 2025/6/16
 */
@Service
public class SellerApplicationServiceImpl implements SellerApplicationService {

    private final SellerRepository sellerRepository;

    private final SellerService sellerService;

    public SellerApplicationServiceImpl(@Qualifier("mockSellerRepository") SellerRepository sellerRepository, SellerService sellerService) {
        this.sellerRepository = sellerRepository;
        this.sellerService = sellerService;
    }

    /**
     * 1.校验与业务解耦
     * 2.应用层统一管理事务与流程编排
     * */

    @Override
    public void addSeller(UserInfoParam userInfoParam) {
        PhoneNumber phone = PhoneNumber.create(userInfoParam.getContactPhone());
        CompanyInfo companyInfo = CompanyInfo.create(userInfoParam.getCompanyAddress(), userInfoParam.getCompanyName(), userInfoParam.getCompanyLicenseId(), userInfoParam.getCompanyLicenseUrl());
        Email email = Email.create(userInfoParam.getContactEmail());
        Seller seller = Seller.createSeller(phone, companyInfo, userInfoParam.getContactWechat(), email, userInfoParam.getBusinessMainImage());
        sellerService.addSeller(seller);
        sellerRepository.saveOrUpdate(seller);
    }
}
